package module.map.nodes.newNode
{
	import drongo.desktop.utils.StringUtil;
	import drongo.desktop.utils.UtilsFile;
	
	import flash.events.Event;
	import flash.filesystem.File;
	import flash.geom.Point;
	import flash.net.FileFilter;
	import flash.text.engine.BreakOpportunity;
	
	import common.view.FGUIMediator;
	import common.window.AlertDialog;
	
	import module.map.Operations;
	import module.map.model.ProjectModel;
	import module.map.model.Propertys;
	
	import ui.Map.UI_NewNodeWindow;
	
	public class NewNodeWindowMediator extends FGUIMediator
	{
		[Inject]
		public var view:NewNodeWindow;
		[Inject]
		public var project:ProjectModel;
		
		public function NewNodeWindowMediator()
		{
			super();
		}
		
		override public function initialize():void{
			super.initialize();
			this.AddEvents();
		}
		
		override public function destroy():void{
			super.destroy();
			this.RemoveEvents();
		}
		
		private function AddEvents():void{
			this.viewCom.m_closeButton.addClickListener(this.CloseButtonClick);
			this.viewCom.m_newButton.addClickListener(this.CreateButtonClick);
			this.viewCom.m_selectButton.addClickListener(this.SelectButtonClick);
		}
		
		private function RemoveEvents():void{
			this.viewCom.m_closeButton.removeClickListener(this.CloseButtonClick);
			this.viewCom.m_newButton.removeClickListener(this.CreateButtonClick);
			this.viewCom.m_selectButton.removeClickListener(this.SelectButtonClick);
		}
		
		private function SelectButtonClick(e:Event):void{
			UtilsFile.browseForOpen("选择需要图片",[new FileFilter("image","*.png;*.jpg")],function(file:File):void
			{
				viewCom.m_imagePath.text=file.nativePath;
			});
		}
		
		private function CloseButtonClick(e:Event=null):void{
			this.view.hideImmediately();
			this.view.dispose();
		}
		
		private function CreateButtonClick(e:Event):void{
			if(StringUtil.IsEmpty(this.viewCom.m_nameInput.text)){
				AlertDialog.open(this.viewCom.root,"名称不能为空！");
				return;
			}
			var localPos:Point=this.viewCom.data as Point;
			switch(this.viewCom.m_typeComboBox.selectedIndex)
			{
				case 0://路径
				{
					this.project.undoSystem.executeOperation(Operations.NEW_NODE,{
						type:this.viewCom.m_typeComboBox.selectedIndex,
						name:this.viewCom.m_nameInput.text,
						pos:localPos});
					break;
				}
				case 1://区域
				{
					if(StringUtil.IsEmpty(this.viewCom.m_areaTypeTxt.text)){
						AlertDialog.open(this.viewCom.root,"区域类型不能为空！");
					}
					var areaType:Number=Number(this.viewCom.m_areaTypeTxt.text);
					if(isNaN(areaType)){
						AlertDialog.open(this.viewCom.root,"区域类型必须是数字！");
					}
					this.project.undoSystem.executeOperation(Operations.NEW_NODE,{
						type:this.viewCom.m_typeComboBox.selectedIndex,
						name:this.viewCom.m_nameInput.text,
						pos:localPos,areaType:areaType});
					break;
				}
				case 2://image
				{
					if(StringUtil.IsEmpty(this.viewCom.m_imagePath.text)){
						AlertDialog.open(this.viewCom.root,"图片地址不能为空！");
					}
					var mapID:String=this.project.currentMap.Get(Propertys.ID).GetValue();
					var srcFile:File=new File(this.viewCom.m_imagePath.text);
					if(!srcFile.exists){
						AlertDialog.open(this.viewCom.root,"图片不存在！");
					}
					//相对路径
					var imagePath:String;
					var mapDic:String=this.project.imagesFile.nativePath+File.separator;
					//如果选择的图片不在工程目录的maps的mapID文件夹下
					if(srcFile.nativePath.indexOf(mapDic)<0){
						var file:File=new File(this.project.imagesFile.nativePath+File.separator+srcFile.name);
						UtilsFile.copyFile(srcFile,file);
						imagePath=file.nativePath.replace(mapDic,"");
					}else{
						imagePath=srcFile.nativePath.replace(mapDic,"");
					}
					this.project.undoSystem.executeOperation(Operations.NEW_NODE,{type:this.viewCom.m_typeComboBox.selectedIndex,
						name:this.viewCom.m_nameInput.text,
						imagePath:imagePath,
						pos:localPos});
					break;
				}
				case 3:
				{
					this.project.undoSystem.executeOperation(Operations.NEW_NODE,{
						type:this.viewCom.m_typeComboBox.selectedIndex,
						towerType:this.viewCom.m_towerTypeComboBox.selectedIndex,
						name:this.viewCom.m_nameInput.text,
						pos:localPos});
					break;
				}
				default:
				{
					throw new Error("未知节点类型！");
					break;
				}
			}
			this.CloseButtonClick();
		}
		
		private function get viewCom():UI_NewNodeWindow{
			return this.view.view;
		}
	}
}